<template>
	<view class="container">
		<!-- 轮播图部分 -->
		<swiper class="banner" circular autoplay interval="3000" duration="500" indicator-dots
			indicator-active-color="#fff" @change="handleSwiperChange">
			<swiper-item v-for="(item, index) in bannerList" :key="index" @tap="handleBannerClick(item)">
				<image :src="item.imageUrl" mode="aspectFill" class="banner-image" />
				<view class="banner-title" v-if="item.title">{{item.title}}</view>
			</swiper-item>
		</swiper>

		<!-- 顶部搜索栏 -->
		<view class="search-bar">
			<input 
				class="search-input" 
				type="text" 
				v-model="keyword" 
				placeholder="搜索课程"
				@confirm="handleSearch"
			/>
		</view>
		
		<!-- 分类标签页 -->
		<scroll-view class="category-tabs" scroll-x>
			<view 
				class="tab-item" 
				v-for="(item, index) in categories" 
				:key="item._id"
				:class="{ active: selectedCategory === item._id }"
				@click="handleTabChange(item._id)"
			>
				<text>{{ item.name }}</text>
			</view>
		</scroll-view>

		<!-- 课程列表 -->
		<scroll-view class="course-list" scroll-y @scrolltolower="loadMore" refresher-enabled
			:refresher-triggered="isRefreshing" @refresherrefresh="onRefresh">
			<view class="course-item" v-for="(course, index) in courseList" :key="index"
				@tap="goToDetail(course._id)">
				<image :src="course.imageUrl" mode="aspectFill" class="course-image" />
				<view class="course-info">
					<view class="course-title">{{ course.title }}</view>
					<view class="course-desc">
						<view class="info-item">
							<text class="iconfont icon-time"></text>
							<text>{{ course.duration }}</text>
						</view>
						<view class="info-item">
							<text class="iconfont icon-location"></text>
							<text>{{ course.location }}</text>
						</view>
					</view>
					<view class="course-bottom">
						<view class="price">
							<text class="symbol">¥</text>
							<text class="number">{{ course.price }}</text>
							<text class="unit">/人起</text>
						</view>
						<view class="book-btn">立即预订</view>
					</view>
				</view>
			</view>
			
			<!-- 加载状态 -->
			<view class="loading-status" v-if="courseList.length > 0">
				<view v-if="isLoading" class="loading">
					<text class="loading-text">加载中...</text>
				</view>
				<view v-else-if="!hasMore" class="no-more">
					<text class="no-more-text">没有更多了</text>
				</view>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="courseList.length === 0 && !isLoading">
				<image src="/static/empty.png" mode="aspectFit" class="empty-image"></image>
				<text class="empty-text">暂无课程</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
// 操作数据库
const db = uniCloud.database()
export default {
	data() {
		return {
			searchKey: '',
			bannerList: [],
			currentBannerIndex: 0,
			courseList: [],
			page: 1,
			pageSize: 10,
			hasMore: true,
			isLoading: false,
			isRefreshing: false,
			// 本地固定的分类列表2
			categories: [
				{ _id: '', name: '全部' },
			//	{ _id: '7', name: '亲子推荐' },
				{ _id: '1', name: '编程开发' },
				{ _id: '2', name: '设计创作' },
				{ _id: '3', name: '语言学习' },
				{ _id: '4', name: '考试认证' },
				{ _id: '5', name: '职场技能' },
				{ _id: '6', name: '兴趣爱好' }
			],
			selectedCategory: '', // 当前选中的分类ID
			keyword: '',
		}
	},

	onLoad() {
		this.getBanners()
		this.getCourseList()
	},
	
	onPullDownRefresh() {
		this.refresh()
	},
	
	methods: {
		// 获取轮播图数据
		async getBanners() {
			uni.showLoading({
				title: '加载中...'
			})
			try {
				const  res  = await uniCloud.callFunction({
					name: 'getBannerList',
					data: {
						page: 1,
						pageSize:20,
						keyword: ''
					}
				})
				console.log(res.result.data)
				if (res.result.code === 0) {
					const { list, total } = res.result.data
					
					this.bannerList = list
					
					
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			} catch (e) {
				uni.showToast({
					title: '获取轮播图数据失败',
					icon: 'none'
				})
			} finally {
				uni.hideLoading()
			}
		},
		
		// 获取课程列表
		async getCourseList(isLoadMore = false) {
			if (this.isLoading) return
			this.isLoading = true
			
			try {
				const { result } = await uniCloud.callFunction({
					name: 'getCourseList',
					data: {
						page: this.page,
						pageSize: this.pageSize,
						searchKey: this.searchKey
					}
				})
				
				if (result.code === 0) {
					if (isLoadMore) {
						this.courseList = [...this.courseList, ...result.data.list]
					} else {
						this.courseList = result.data.list
					}
					this.hasMore = result.data.hasMore
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			} catch (e) {
				uni.showToast({
					title: '获取课程列表失败',
					icon: 'none'
				})
			} finally {
				this.isLoading = false
				this.isRefreshing = false
				uni.stopPullDownRefresh()
			}
		},
		
		// 下拉刷新
		async onRefresh() {
			this.isRefreshing = true
			this.page = 1
			await this.getCourseList()
		},
		
		// 上拉加载更多
		loadMore() {
			if (!this.hasMore || this.isLoading) return
			this.page++
			this.getCourseList(true)
		},
		
		// 搜索
		handleSearch() {
			this.page = 1
			this.getCourseList()
		},
		
		// 清空搜索
		clearSearch() {
			this.searchKey = ''
			this.page = 1
			this.getCourseList()
		},
		
		handleSwiperChange(e) {
			this.currentBannerIndex = e.detail.current
		},
		
		handleBannerClick(banner) {
			if (!banner) return
			
			// 根据banner类型处理不同的跳转逻辑
			switch(banner.type) {
				case 'course':
					// 跳转到课程详情
					uni.navigateTo({
						url: `/pages/course/detail?id=${banner.targetId}`
					})
					break
				case 'url':
					// 跳转到外部链接
					if (banner.link) {
						// #ifdef H5
						window.open(banner.link)
						// #endif
						// #ifdef APP-PLUS
						plus.runtime.openURL(banner.link)
						// #endif
					}
					break
				case 'page':
					// 跳转到内部页面
					if (banner.link) {
						uni.navigateTo({
							url: banner.link
						})
					}
					break
			}
		},
		
		// 跳转到课程详情
		goToDetail(id) {
			console.log(id);
			uni.navigateTo({
				url: `/pages/course/detail?id=${id}`
			});
		},
		
		// 分类切换
		handleTabChange(categoryId) {
			this.selectedCategory = categoryId
			this.page = 1
			this.hasMore = true
			this.loadCourseList(true)
		},
		
		// 加载课程列表
		async loadCourseList(isRefresh = false) {
			try {
				const res = await uniCloud.callFunction({
					name: 'getCourseList',
					data: {
						page: this.page,
						pageSize: this.pageSize,
						keyword: this.keyword,
						categoryId: this.selectedCategory || ''
					}
				})
				
				if (res.result.code === 0) {
					if (isRefresh) {
						this.courseList = res.result.data.list
					} else {
						this.courseList = [...this.courseList, ...res.result.data.list]
					}
					this.hasMore = res.result.data.hasMore
				} else {
					uni.showToast({
						title: res.result.msg,
						icon: 'none'
					})
				}
			} catch (e) {
				uni.showToast({
					title: '获取课程列表失败',
					icon: 'none'
				})
			} finally {
				this.isLoading = false
				this.isRefreshing = false
				uni.stopPullDownRefresh()
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background-color: #f8f9fc;
	display: flex;
	flex-direction: column;

	.banner {
		height: 400rpx;
		position: relative;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);

		.banner-image {
			width: 100%;
			height: 100%;
			border-radius: 0 0 30rpx 30rpx;
		}

		.banner-title {
			position: absolute;
			bottom: 30rpx;
			left: 30rpx;
			right: 30rpx;
			color: #fff;
			font-size: 32rpx;
			font-weight: bold;
			text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
			background: linear-gradient(transparent, rgba(0, 0, 0, 0.5));
			padding: 20rpx;
			border-radius: 16rpx;
		}
	}

	.search-bar {
		margin: -40rpx 30rpx 0;
		position: relative;
		z-index: 1;

		.search-input {
			display: flex;
			align-items: center;
			background: #fff;
			padding: 20rpx 30rpx;
			border-radius: 50rpx;
			box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);

			.icon-search {
				font-size: 36rpx;
				color: #999;
				margin-right: 20rpx;
			}

			input {
				flex: 1;
				height: 60rpx;
				font-size: 28rpx;
			}

			.icon-close {
				font-size: 36rpx;
				color: #999;
				padding: 10rpx;
			}
		}
	}

	.category-tabs {
		white-space: nowrap;
		background-color: #fff;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.tab-item {
		display: inline-block;
		padding: 0 30rpx;
		height: 60rpx;
		line-height: 60rpx;
		margin: 0 10rpx;
		font-size: 28rpx;
		color: #666;
		position: relative;
	}

	.tab-item.active {
		color: #4a6ee0;
		font-weight: bold;
	}

	.tab-item.active::after {
		content: '';
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		width: 40rpx;
		height: 4rpx;
		background-color: #4a6ee0;
		border-radius: 2rpx;
	}

	.course-list {
		flex: 1;
		padding: 30rpx;
		box-sizing: border-box;

		.course-item {
			background: #fff;
			border-radius: 20rpx;
			margin-bottom: 30rpx;
			overflow: hidden;
			box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
			transition: all 0.3s;

			&:active {
				transform: scale(0.98);
				opacity: 0.9;
			}

			.course-image {
				width: 100%;
				height: 360rpx;
			}

			.course-info {
				padding: 30rpx;

				.course-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
					margin-bottom: 20rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}

				.course-desc {
					display: flex;
					flex-direction: column;
					gap: 16rpx;
					margin-bottom: 30rpx;

					.info-item {
						display: flex;
						align-items: center;
						font-size: 26rpx;
						color: #666;

						.iconfont {
							font-size: 32rpx;
							margin-right: 10rpx;
							color: #4a6ee0;
						}
					}
				}

				.course-bottom {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.price {
						.symbol {
							font-size: 24rpx;
							color: #ff4d4f;
						}

						.number {
							font-size: 36rpx;
							font-weight: bold;
							color: #ff4d4f;
							margin: 0 4rpx;
						}

						.unit {
							font-size: 24rpx;
							color: #999;
						}
					}

					.book-btn {
						background: linear-gradient(135deg, #4a6ee0, #3a5bc7);
						color: #fff;
						font-size: 28rpx;
						padding: 16rpx 40rpx;
						border-radius: 40rpx;
					}
				}
			}
		}

		.loading-status {
			padding: 30rpx 0;
			text-align: center;

			.loading {
				.loading-text {
					font-size: 26rpx;
					color: #999;
				}
			}

			.no-more {
				.no-more-text {
					font-size: 26rpx;
					color: #999;
				}
			}
		}

		.empty-state {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 100rpx 0;

			.empty-image {
				width: 240rpx;
				height: 240rpx;
				margin-bottom: 30rpx;
			}

			.empty-text {
				font-size: 28rpx;
				color: #999;
			}
		}
	}
}
</style>
